<?php

// 主要是用来测试文件上传
class Default_WsdlController extends Cff_Controller_Action {
	public function preDispatch() {
		$this->domain = 'http://' . $_SERVER ['SERVER_NAME'];
	}
	public function soapAction() {
		// initialize server and set URI
		$server = new Zend_Soap_Server ( $this->domain . "/default/soap/wsdl/" );
		
		// set SOAP service class
		$server->setClass ( 'Default_Service_Soap' );
		
		// register exceptions that generate SOAP faults
		$server->registerFaultException ( array (
				'Exception' 
		) );
		// handle request
		$server->handle ();
	}
	
	public function indexAction() {
		
		echo $this->view->render ( "upload.phtml" ); // 视图文件为完整网页 不使用layout
	}
	
	// 图片上传
	public function uploadAction() {
		$upload = new Zend_File_Transfer_Adapter_Http ();
		// 设置上传之后文件的目录
		$upload->setDestination ( '../data/upload' );
		
		$upload->addValidator ( 'Extension', false, 'gif,jpeg,png,jpg' )->		// 设置上传文件的后缀名
		addValidator ( 'Count', false, array (
				'min' => 1, 
				'max' => 5 
		) )->		// 设置上传文件的个数
		addValidator ( 'ImageSize', false, array (
				'minwidth' => 0,  // 设置上传图片的大小
				'maxwidth' => 1300, 
				'minhight' => 0, 
				'maxhight' => 1300 
		) )->addValidator ( 'FilesSize', false, array (
				'min' => '10KB', 
				'max' => '1024KB' 
		) ); // 设置上传文件的大小
		                       
		// 添加过滤器来修改上传文件的名称
		                       // $upload->addFilter('Rename',array('target'=>'images/123.gif','overwrite'=>true));
		
// 		print_r ( $upload->getFileInfo () );

		$fileInfo = $upload->getFileInfo ();
		
		$files = array ();
		foreach ( $fileInfo as $file => $info ) {
			if ($upload->isValid ( $file )) {
				
				$fileName = time () . mb_convert_encoding ( $info ['name'], 'gbk', 'utf-8' );
				$upload->addFilter ( 'Rename', array (
						'target' => $fileName, 
						
						'overwrite' => true 
				), $file );
				$files [] =array (
									"fileName" => $fileName, 
									"data" => base64_encode ( file_get_contents ( $info ['tmp_name'] ) ) 
							) ;

				
				$upload->receive ( $file );
			} else {
				// 对不能上传的文件的错误信息进行处理
				print_r ( $upload->getMessages () ); // 得到错误信息
			}
		}
		
		// initialize SOAP client
		try {
			$client = new Zend_Soap_Client ( $this->domain . "/default/soap/wsdl/" );
			$req = array (
					'HeaderRequest' => array (
							'customerId' => 'A0256', 
							'appToken' => 'asdffffffffffffffffffffffffasdfasdfasdfasdfasdfaffffffffffffffffffffffffffffffddddddddddddddddd', 
							'appKey' => 'ffdddfdfdeaadffffffffffffffasdf' 
					), 
					"files" => $files
			);
			
			$result = $client->uploadProduct ( $req );
			print_r ( $result );
			var_dump($client->getLastRequest());
			var_dump($client->getLastResponse());
			
		} catch ( SoapFault $s ) {
			die ( 'ERROR: [' . $s->faultcode . '] ' . $s->faultstring );
		} catch ( Exception $e ) {
			die ( 'ERROR: ' . $e->getMessage () );
		}
		$this->view->msg = print_r($upload->getFileInfo (),true).print_r($result,true);
		
		echo $this->view->render ( "upload.phtml" ); // 视图文件为完整网页 不使用layout
	}
}